<template>
  <div class="main-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>商品审核</span>
      </div>
      
      <el-tabs v-model="activeTab" @tab-click="handleTabClick">
        <el-tab-pane label="待审核" name="0">
          <product-table :status="0"></product-table>
        </el-tab-pane>
        <el-tab-pane label="已通过" name="1">
          <product-table :status="1"></product-table>
        </el-tab-pane>
        <el-tab-pane label="已驳回" name="2">
          <product-table :status="2"></product-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <!-- 审核对话框 -->
    <el-dialog :title="reviewTitle" :visible.sync="dialogVisible" width="500px">
      <el-form :model="reviewForm" ref="reviewForm" label-width="80px">
        <el-form-item label="审核结果" prop="status">
          <el-radio-group v-model="reviewForm.status">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="2">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="驳回原因" v-if="reviewForm.status === 2" prop="reason">
          <el-input type="textarea" v-model="reviewForm.reason"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitReview">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ProductTable from './components/ProductTable'

export default {
  components: {
    ProductTable
  },
  data() {
    return {
      activeTab: '0',
      dialogVisible: false,
      currentProduct: null,
      reviewForm: {
        status: 1,
        reason: ''
      }
    }
  },
  computed: {
    reviewTitle() {
      return `审核商品: ${this.currentProduct?.name || ''}`
    }
  },
  methods: {
    handleTabClick() {
      this.$refs.productTable.getList()
    },
    openReview(product) {
      this.currentProduct = product
      this.dialogVisible = true
      this.reviewForm = {
        status: 1,
        reason: ''
      }
    },
    submitReview() {
      if (this.reviewForm.status === 2 && !this.reviewForm.reason) {
        this.$message.warning('请输入驳回原因')
        return
      }
      
      const data = {
        id: this.currentProduct.id,
        ...this.reviewForm
      }
      
      this.$http.post(`${this.currentProduct.table}/review`, data).then(({data}) => {
        if (data && data.code === 0) {
          this.$message.success('审核成功')
          this.dialogVisible = false
          this.$refs.productTable.getList()
        } else {
          this.$message.error(data.msg)
        }
      })
    }
  }
}
</script>